package ru.fadeev.crammer.ui.window;


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import ru.fadeev.crammer.Settings;
import ru.fadeev.crammer.data.Dictionary;
import ru.fadeev.crammer.ui.LimitedPlainDocument;


/**
 * SpecifyDictionaryNameDialog class provides GUI for entering dictionary name
 * when adding new dictionary or renaming existing.
 * @author Konstantin Fadeyev
 *
 */
public class SpecifyDictionaryNameDialog extends JDialog {

	// Constants
	public final static int ADD_MODE = 0;
	public final static int EDIT_MODE = 1;
	
	public final static int BTN_OK = 100;
	public final static int BTN_CANCEL = 101;
	
	// UI Components
    private JButton cancelButton;
	private JLabel dictionaryNameLabel;
	private JTextField dictionaryNameTextField;	
	private JPanel mainPanel;
    private JButton okButton;
    
	// User fields
    private Dictionary dictionary;
	private int pressedButton;   
	
	
	/**
	 * Constructs SpecifyDictionaryNameDialog and initializes it with 
	 * dictionary and mode
	 * @param owner parent window
	 * @param dictionary dictionary we specifying name for
	 * @param mode one of ADD_MODE, EDIT_MODE
	 */
    public SpecifyDictionaryNameDialog(Frame owner, Dictionary dictionary, int mode) {
        super(owner,true);
        this.dictionary = dictionary;     
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(410, 100);
        setLocation((screenSize.width-410)/2, (screenSize.height-100)/3);
        
        setResizable(false);
        if(mode == ADD_MODE) {
        	setTitle("Add dictionary");
        } else {
        	setTitle("Rename dictionary");
        }
        pressedButton = BTN_CANCEL;
        
        componentsInit();
        placeComponents();
    }

    
    /**
     * Cancel button action hadler. Closes dialog.
     */
    private void cancelButtonAction() {
        pressedButton = BTN_CANCEL;
        setVisible(false);
    }
   
    
    /**
     * Initializes UI components
     */
    private void componentsInit() {
        mainPanel = new JPanel();
        dictionaryNameLabel = new JLabel("Dictionary name:");  
        dictionaryNameTextField = new JTextField();
    	LimitedPlainDocument dictionaryNameDocument = 
			new LimitedPlainDocument(Settings.DICTIONARY_NAME_MAX_LENGTH);
    	dictionaryNameTextField.setDocument(dictionaryNameDocument);
    	dictionaryNameTextField.setText(dictionary.getName());
        okButton = new JButton("OK");
        getRootPane().setDefaultButton(okButton);
        
		cancelButton = new JButton("Cancel");
        okButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	okButtonAction();
			        }
			    });
        
        cancelButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	cancelButtonAction();
			        }
		        });
        
        setContentPane(mainPanel);
    }
    
    
    /**
     * Overwritten to register escape button on closing window action
     */
    protected JRootPane createRootPane() {
	    ActionListener actionListener = new ActionListener() {
	    	public void actionPerformed(ActionEvent actionEvent) {
	    		setVisible(false);
	    	}
	    };
	    JRootPane rootPane = new JRootPane();
	    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
	    rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
	    return rootPane;
    }
    
    
    /**
     * After dialog is closed, call this function to find out which option
     * user picked (OK or Cancel)
     * @return pressed button (one of BTN_OK, BTN_CANCEL)
     */
    public int getPressedButton() {
    	return pressedButton;
    }
    
    /**
     * Ok button action handler. Validates entered values.
     */
    private void okButtonAction() { 
    	if(dictionaryNameTextField.getText().trim().equals("")) {
    		JOptionPane.showMessageDialog(this, 
    				"Enter dictionary name to continue.",
    				"Warning", JOptionPane.WARNING_MESSAGE);
    		dictionaryNameTextField.requestFocus();
    		return;
    	}    	
    	dictionary.setName(dictionaryNameTextField.getText().trim());
    	pressedButton = BTN_OK;
    	    	
    	setVisible(false);
    }
    
    
    /**
     * Places UI components using different layout managers
     */
    private void placeComponents() {
    	mainPanel.setLayout(null);
    	mainPanel.add(dictionaryNameLabel);
    	mainPanel.add(dictionaryNameTextField);
    	mainPanel.add(okButton);
    	mainPanel.add(cancelButton);  
    	  	
    	dictionaryNameLabel.setBounds(10, 10, 90, 20);
    	dictionaryNameTextField.setBounds(110, 10, 280, 20);
    		
    	okButton.setBounds(200, 40, 90, 20);
    	cancelButton.setBounds(300, 40, 90, 20);

    }
}
